package com.ruicar.afs.cloud.apply.business.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.business.condition.WorkflowRecordDetailsCondition;
import com.ruicar.afs.cloud.apply.business.mapper.ApplyRecordDetailsMapper;
import com.ruicar.afs.cloud.apply.business.service.ApplyRecordDetailsService;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCarDetails;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOprRecord;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOrderInfo;
import com.ruicar.afs.cloud.apply.common.entity.WorkflowRecordDetails;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCarDetailsService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOprRecordService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.vo.WorkRecordDetailsVO;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyCostDetailsService;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.CostTypeEnum;
import com.ruicar.afs.cloud.parameter.commom.enums.AProveBusinessTypeEnum;
import com.ruicar.afs.cloud.parameter.commom.enums.CostType;
import com.ruicar.afs.cloud.parameter.commom.enums.OrderOprType;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date: 2020/5/16 16:45
 * @description: 审批记录service实现
 */
@Service
@AllArgsConstructor
public class ApplyRecordDetailsServiceImpl extends ServiceImpl<ApplyRecordDetailsMapper, WorkflowRecordDetails> implements ApplyRecordDetailsService {
    private final ApplyCostDetailsService applyCostDetailsService;
    private final ApplyCarDetailsService applyCarDetailsService;
    private final ApplyOrderInfoService applyOrderInfoService;
    private final ApplyOprRecordService applyOprRecordService;

    /**
     * 获取审批记录信息
     *
     * @param condition
     * @return
     */
    @Override
    public WorkRecordDetailsVO getRecordInfo(WorkflowRecordDetailsCondition condition) {
        String businessNo = condition.getBusinessNo();
        WorkRecordDetailsVO params = new WorkRecordDetailsVO();
        //务类型为：复议/资产变更
        if (StringUtil.isNotEmpty(condition.getBusinessType())) {
            if (String.valueOf(OrderOprType.ASSERTCHANGE).equals(condition.getBusinessType())
                    || String.valueOf(OrderOprType.RECONSIDER).equals(condition.getBusinessType())) {
                //根据申请编号查询操作表信息,获取操作表id
                List<ApplyOprRecord> applyOprRecordList = applyOprRecordService.list(Wrappers.<ApplyOprRecord>query().lambda()
                        .eq(StringUtil.isNotEmpty(businessNo), ApplyOprRecord::getApplyNo, businessNo).last(""));
                if (CollectionUtil.isNotEmpty(applyOprRecordList)) {
                    //根据操作表id查询复议/审批历史记录
                    List<WorkflowRecordDetails> recordDetailsList = list(Wrappers.<WorkflowRecordDetails>query().lambda()
                            .eq(WorkflowRecordDetails::getBusinessNo, String.valueOf(applyOprRecordList.get(applyOprRecordList.size() - 1).getId()))
                            .eq(WorkflowRecordDetails::getBusinessType, condition.getBusinessType()));
                    params = this.getParams(recordDetailsList, businessNo);
                    return params;
                }
            }
        } else {
            //审批记录,常规审批，传的申请编号
            List<WorkflowRecordDetails> recordDetailsList = list(Wrappers.<WorkflowRecordDetails>query().lambda()
                    .eq(StringUtil.isNotEmpty(condition.getBusinessNo()), WorkflowRecordDetails::getBusinessNo, condition.getBusinessNo())
                    .eq(StringUtil.isNotEmpty(condition.getBusinessType()), WorkflowRecordDetails::getBusinessType, condition.getBusinessType())
                    .eq(StringUtil.isNotEmpty(condition.getApproveResult()), WorkflowRecordDetails::getApproveResult, condition.getApproveResult()));
            params = this.getParams(recordDetailsList, businessNo);
            return params;
        }
        return params;
    }

    /**
     * 审批历史记录封装
     *
     * @param recordDetailsList
     * @param bussinessNo
     * @return
     */
    public WorkRecordDetailsVO getParams(List<WorkflowRecordDetails> recordDetailsList, String bussinessNo) {
        WorkRecordDetailsVO workRecordDetailsVO = new WorkRecordDetailsVO();
        List<WorkflowRecordDetails> detailsList = new ArrayList<>();
        //审批记录
        if (CollectionUtil.isNotEmpty(recordDetailsList)) {
            for (WorkflowRecordDetails workflowRecordDetails : recordDetailsList) {
                if (AProveBusinessTypeEnum.PENDING_REVIEW.getCode().equals(workflowRecordDetails.getApproveResult())) {
                    workflowRecordDetails.setApproveResultName(AProveBusinessTypeEnum.PENDING_REVIEW.getDesc());
                } else if (AProveBusinessTypeEnum.AUDIT.getCode().equals(workflowRecordDetails.getApproveResult())) {
                    workflowRecordDetails.setApproveResultName(AProveBusinessTypeEnum.AUDIT.getDesc());
                } else if (AProveBusinessTypeEnum.APPROVAL.getCode().equals(workflowRecordDetails.getApproveResult())) {
                    workflowRecordDetails.setApproveResultName(AProveBusinessTypeEnum.APPROVAL.getDesc());
                } else if (AProveBusinessTypeEnum.REJECTION.getCode().equals(workflowRecordDetails.getApproveResult())) {
                    workflowRecordDetails.setApproveResultName(AProveBusinessTypeEnum.REJECTION.getDesc());
                } else if (AProveBusinessTypeEnum.BACK.getCode().equals(workflowRecordDetails.getApproveResult())) {
                    workflowRecordDetails.setApproveResultName(AProveBusinessTypeEnum.BACK.getDesc());
                } else if (AProveBusinessTypeEnum.CANCELLATION.getCode().equals(workflowRecordDetails.getApproveResult())) {
                    workflowRecordDetails.setApproveResultName(AProveBusinessTypeEnum.CANCELLATION.getDesc());
                }
                if (StringUtil.isEmpty(workflowRecordDetails.getApproveRemarks())) {
                    workflowRecordDetails.setApproveRemarks("");
                }
                detailsList.add(workflowRecordDetails);
            }
            if (StringUtil.isNotEmpty(bussinessNo)) {
                //订单信息
                ApplyOrderInfo orderInfoByApplyNo = applyOrderInfoService.getOrderInfoByApplyNo(bussinessNo);
                //融资信息
                List<FinCostDetails> costDetailsList = applyCostDetailsService.getCostDetailsList(bussinessNo);
//                BigDecimal totalContractAmt = BigDecimal.ZERO;
                if (CollectionUtil.isNotEmpty(costDetailsList)) {
                    for (FinCostDetails finCostDetails : costDetailsList) {
                        //车款
                        if (CostTypeEnum.CARAMT.getCode().equals(finCostDetails.getCostType())) {
                            if (finCostDetails.getAddAmt() != null) {
                                finCostDetails.setContractAmt(finCostDetails.getContractAmt().add(finCostDetails.getAddAmt()));
//                                totalContractAmt = totalContractAmt.add(finCostDetails.getContractAmt());
                            }
                        } else if (CostTypeEnum.AFFIXAMT.getCode().equals(finCostDetails.getCostType())) {
                            //附加贷
                            if (finCostDetails.getAddAmt() != null) {
                                finCostDetails.setContractAmt(finCostDetails.getContractAmt().add(finCostDetails.getAddAmt()));
//                                totalContractAmt = totalContractAmt.add(finCostDetails.getContractAmt());
                            }
                        }
                    }
                    workRecordDetailsVO.setCostDetailsList(costDetailsList);
                    workRecordDetailsVO.setCustName(orderInfoByApplyNo.getCustName());
                    if (StringUtil.isNotEmpty(orderInfoByApplyNo.getRemarks())) {
                        workRecordDetailsVO.setReaMarks(orderInfoByApplyNo.getRemarks());
                    } else {
                        workRecordDetailsVO.setReaMarks("");
                    }
                }
                //车辆详情
                ApplyCarDetails carDetailsByApplyNo = applyCarDetailsService.getCarDetailsByApplyNo(bussinessNo);
                if (ObjectUtil.isNotNull(carDetailsByApplyNo)) {
                    workRecordDetailsVO.setApplyCarDetails(carDetailsByApplyNo);
                }
            }
            workRecordDetailsVO.setDetailsList(detailsList);
        } else {
            workRecordDetailsVO.setFlag("false");
        }
        return workRecordDetailsVO;
    }
}
